import { BaseWindow } from "../../../../component/window/BaseWindow";
import { ILayer } from "../../../../mvc/interfaces/ILayer";
import { Language } from "../../../../../extend/language/Language";
import { ObjCreate } from "../../common/ObjCreate";
import { GameConst } from "../../../../component/gconst/GameConst";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { PageSelecter } from "../../common/pageSelect/PageSelecter";
import { HTMLUtil } from "../../../../../com/gengine/utils/HTMLUtil";
import { TilePanel } from "../../equipment/TilePanel";
import { MountChangeCellRenderer } from "../mount/MountChangeCellRenderer";
import { ScrollBarDirection } from "../../../../../fl/controls/ScrollBarDirection";
import { ScrollPolicy } from "../../../../../fl/controls/ScrollPolicy";
import { ListEvent } from "../../../../../fl/events/ListEvent";
import { MountModelConfig } from "../../../resource/MountModelConfig";
import { MsgManager } from "../../../manager/MsgManager";
import { Cache } from "../../../cache/Cache";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { ItemData } from "../../../resource/info/ItemData";
import { DateParser } from "../../../../common/tools/DateParser";
import { ClockManager } from "../../../manager/ClockManager";
import { MountConfig } from "../../../resource/MountConfig";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { _changeModelId } from "../../../../../this/_changeModelId";
type int = number;
//class MountChangeModelWindow
    
    export  class MountChangeModelWindow extends BaseWindow
    {
        constructor(arg1: ILayer=null)
        {
            super(arg1);
            this._collectArr = [];
            this._totalModelArr = [];
            this._showModelArr = [];
            setSize(646, 483);
            titleHeight = 30;
            title = Language.getString(70128);
            return;
        }

        protected /* override */ createChildren(): void
        {
            super.createChildren();
            ObjCreate.createBg(17, 41, 612, 434, this);
            var loc1=ObjCreate.createTextField("", 114, 44, 450, 20, this);
            loc1.htmlText = Language.getString(70125);
            var loc2=ObjCreate.createTextField("", 140, 448, 90, 20, this);
            loc2.htmlText = Language.getString(70127);
            this._costNibus = GameConst.MountChangeModelNeedNimbus;
            this._limitTime = GameConst.MountChangeModelTimeLimit;
            this._limitSecond = GameConst.MountChangeModelTimeLimitEx * 1000;
            var loc3=ObjCreate.createTextField(this._costNibus + Language.getString(70037), 220, 448, 60, 20, this, GlobalStyle.textFormatGreen);
            this._pageSelec = ObjCreate.createPageSelecter(278, 449, this, PageSelecter.CompleteMode);
            this._pageSelec.addEventListener(flash.events.Event.CHANGE, this.onPageSeleClick);
            this._btnChange = ObjCreate.createGButton(Language.getString(70126), 548, 444, 70, 26, this);
            this._btnChange.toolTipData = HTMLUtil.addColor(Language.getString(70131), "#FFFFFF");
            this._btnChange.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnChange);
            this._showList = new TilePanel();
            this._showList.horizontalGap = 5;
            this._showList.verticalGap = 4;
            this._showList.setSize(603, 378);
            this._showList.columnWidth = 196;
            this._showList.rowHeight = 186;
            this._showList.setStyle("cellRenderer", MountChangeCellRenderer);
            this._showList.direction = ScrollBarDirection.VERTICAL;
            this._showList.scrollPolicy = ScrollPolicy.OFF;
            this._showList.move(23, 66);
            this.addChild(this._showList);
            this._showList.addEventListener(ListEvent.ITEM_CLICK, this.onListItemClickHandler);
            this._totalModelArr = MountModelConfig.instance.getTotalModel().concat();
            this._todayChangeTime = ObjCreate.createTextField("", 28, 448, 220, 20, this);
            return;
        }

        protected onBtnChange(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._mountIData && !(this._changeModelId == "")) 
            {
                this._changeModelId = this._showList.selectedItem.modelId;
                if (this._mountIData.itemExInfo.model_id == this._changeModelId) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(70138));
                    this.hide();
                    return;
                }
                if (this._costNibus > Cache.instance.role.roleInfo.nimbus) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(70133));
                    return;
                }
                if (this._changedCount == this._limitTime) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(70132));
                    return;
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.MountGoChangeSuccess, {"posType": this._mountIData.posType, "mountUid": this._mountIData.uid, "modelId": _changeModelId}));
            }
            return;
        }

        protected onListItemClickHandler(arg1: ListEvent): void
        {
            var loc1=arg1.item as Object;
            this._changeModelId = loc1.modelId;
            return;
        }

        protected onPageSeleClick(arg1: Object /* flash.events.Event */=null): void
        {
            this.updateShowModel(this._showModelArr);
            return;
        }

        /* internal  */updateShowModel(arg1: Array<any>): void
        {
            this._pageSelec.maxPage = Math.ceil(arg1.length / this._pagesize);
            var loc1=(this._pageSelec.currentPage - 1) * this._pagesize;
            var loc2=this._pageSelec.currentPage * this._pagesize > arg1.length ? arg1.length : this._pageSelec.currentPage * this._pagesize;
            var loc3=arg1.slice(loc1, loc2);
            this._showList.dataProvider = new DataProvider(loc3);
            return;
        }

        public setData(arg1: ItemData): void
        {
            var loc2=null;
            var loc3=0;
            var loc4=0;
            var loc5=null;
            var loc6=null;
            var loc7=0;
            var loc8=0;
            var loc9=0;
            var loc10=0;
            var loc11=0;
            var loc12=0;
            this._showModelArr = [];
            this._mountIData = arg1;
            var loc1=0;
            while (loc1 < this._totalModelArr.length) 
            {
                this._totalModelArr[loc1].isLock = true;
                this._totalModelArr[loc1].dt = "";
                ++loc1;
            }
            if (arg1) 
            {
                this._collectArr = Cache.instance.pack.mountsPackCache.getMountcollects(arg1.itemExInfo.m_collects);
                this._changedCount = arg1.itemExInfo.m_change_c;
                this._lastDate = DateParser.strToDateNormal(this._mountIData.itemExInfo.l_change_dt);
                loc2 = ClockManager.instance.nowDate;
                if (!(this._lastDate.getMonth() === loc2.getMonth()) || !(this._lastDate.getFullYear() === loc2.getFullYear()) || !(this._lastDate.getDate() === loc2.getDate())) 
                {
                    this._changedCount = 0;
                }
                this._todayChangeTime.htmlText = Language.getStringByParam(70134, GameConst.MountChangeModelTimeLimit - this._changedCount);
                loc3 = this._collectArr.length;
                loc4 = this._totalModelArr.length;
                if (loc3 > 0) 
                {
                    loc7 = 0;
                    while (loc7 < loc3) 
                    {
                        loc5 = this._collectArr[loc7];
                        loc12 = 0;
                        while (loc12 < loc4) 
                        {
                            loc6 = this._totalModelArr[loc12].modelId;
                            if (loc5.modleId == loc6 && MountConfig.instance.checkModelDt(loc5.modleId, this._mountIData)) 
                            {
                                this._totalModelArr[loc12].isLock = false;
                                this._totalModelArr[loc12].dt = MountConfig.instance.getDt(loc5.modleId, this._mountIData);
                                this._totalModelArr[loc12].tfMark = loc5.tfMark;
                            }
                            ++loc12;
                        }
                        ++loc7;
                    }
                    this._totalModelArr.sortOn("isLock");
                    loc8 = 0;
                    while (loc8 < this._totalModelArr.length) 
                    {
                        if (this._totalModelArr[loc8].isLock == false) 
                        {
                            this._showModelArr.push(this._totalModelArr[loc8]);
                        }
                        ++loc8;
                    }
                    this._showModelArr.sortOn("tfMark", Array.NUMERIC);
                    if ((loc9 = Math.ceil(this._showModelArr.length / 6)) == 0) 
                    {
                        loc9 = 1;
                    }
                    loc10 = loc9 * 6 - this._showModelArr.length;
                    loc11 = 0;
                    while (loc11 < loc10) 
                    {
                        this._showModelArr.push(null);
                        ++loc11;
                    }
                    this.updateShowModel(this._showModelArr);
                }
            }
            return;
        }

        /* internal */ /* var */_pageSelec: PageSelecter;

        /* internal */ /* var */_btnChange: GButton;

        /* internal */ /* var */_costNibus: int;

        /* internal */ /* var */_showList: TilePanel;

        /* internal */ /* var */_mountIData: ItemData;

        /* internal */ /* var */_collectArr: Array<any>;

        /* internal */ /* var */_changedCount: int;

        /* internal */ /* var */_lastDate: Date;

        /* internal */ /* var */_totalModelArr: Array<any>;

        /* internal */ /* var */_pagesize: int=6;

        /* internal */ /* var */_todayChangeTime: Object /* flash.text.TextField */;

        /* internal */ /* var */_changeModelId: string="";

        /* internal */ /* var */_limitTime: int;

        /* internal */ /* var */_limitSecond: int;

        /* internal */ /* var */_showModelArr: Array<any>;
    }
